﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SpyTxT
{
    /// <summary>
    /// 国联物流封包处理
    /// </summary>
    class GuoLianPackManger
    {
        //             0232F8FC  62 65 67 00 01 00 04 31 36 31 37 00 04 31 36 31  beg..1617.161
        //             0232F90C  37 00 08 39 51 47 32 43 4E 5A 56 65 6E 64 00 00  7.9QG2CNZVend..
        public Byte sendflag = 0;
        public System.Byte Sendflag
        {
            get { return sendflag; }
            set { sendflag = value; }
        }
        /// <summary>
        /// 初始化登陆数据包
        /// </summary>
        /// <param name="user">账号</param>
        /// <param name="pass">密码</param>
        /// <returns></returns>
        /// 
        public Byte[] InitLogin(string user, string pass,ref int buflen)
        {
            Byte[] buf = new Byte[0x100];

            int srcoffset = 0;
            String strbeg = "beg";
            Encoding ascii = Encoding.ASCII;
           

            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(strbeg)), 0, buf, srcoffset, strbeg.Length);
            srcoffset += strbeg.Length;

            Byte[] appdata = { 0, 1 };
            Buffer.BlockCopy( appdata, 0,buf, srcoffset, appdata.Length);
            srcoffset += appdata.Length;

            Byte[] userlen = {0,Convert.ToByte(user.Length)};
            Buffer.BlockCopy( userlen, 0,buf, srcoffset, userlen.Length);
            srcoffset += userlen.Length;

            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(user)), 0, buf, srcoffset, user.Length);
            srcoffset += user.Length;

            Byte[] passlen = {0,Convert.ToByte(pass.Length)};
            Buffer.BlockCopy(passlen, 0, buf, srcoffset, passlen.Length);
            srcoffset += passlen.Length;

            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(pass)), 0, buf, srcoffset, pass.Length);
            srcoffset += pass.Length;

            Byte[] computerkeylen = {0,8};
            Buffer.BlockCopy(computerkeylen, 0, buf, srcoffset, computerkeylen.Length);
            srcoffset += computerkeylen.Length;

            string computerkey = "9QG2CNZV";
            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(computerkey)), 0, buf, srcoffset, computerkey.Length);
            srcoffset += computerkey.Length;

            string end = "end";
            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(end)), 0, buf, srcoffset, end.Length);
            srcoffset += end.Length;

            buflen = srcoffset;

            
            return buf;
 
        }

        public Byte[] InitLogin2(ref int buflen)
        {
            Byte[] buf = new Byte[0x8];
            int desoffset = 0;

            string strbeg = "beg";
            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(strbeg)), 0, buf, desoffset, strbeg.Length);
            desoffset += strbeg.Length;

            Byte[] appdata = { 0, 8 };
            Buffer.BlockCopy(appdata, 0, buf, desoffset, appdata.Length);
            desoffset += appdata.Length;

            string strend = "end";
            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(strend)), 0, buf, desoffset, strend.Length);
            desoffset += strend.Length;
            buflen = desoffset;

            return buf;
        }

        public Byte[] InitCRC(ref int buflen)
        {
            Byte[] buf = new Byte[0x100];

            //string data = "0E6E00000002262B1100043136313700000000000000000000000000000000560AF3661239514732434E5A562D373630362D303030300000000000000000000000000431363137000000000B3139322E3136382E312E320000000004BDF0BBAA000000000000066A696E68756100000000";
              string data = "0E6E00000002262B1100043137313600000000000000000000000000000000560AF3661238514732434E5A562D373630362D303030300000000000000000000000000431373136000000000B3139322E3136382E312E320000000004BDF0BBAA000000000000066A696E68756100000000";
            Byte[] senddata = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(data));

            int outlen  = 0;
            ToStr(senddata, ref buf, ref outlen);
            buflen = outlen;
            return buf;

        }

        public Byte[] InitloginCRC(ref int buflen)
        {
            Byte[] buf = new Byte[0x400];

            //string data = "00D1000000043136313700000000000000000000000000000000000000000431363137000000001239514732434E5A562D373630362D30303030000000000000000000000000262B00000B3139322E3136382E312E32000000000D32303032303131302D303330390000000000000000004E0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
            //帐号 1716 硬解码 8QG2CNZV
              string data = "00D1000000043137313600000000000000000000000000000000000000000431373136000000001238514732434E5A562D373630362D30303030000000000000000000000000262B00000B3139322E3136382E312E32000000000D32303032303131302D303330390000000000000000004E0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
            Byte[] senddata = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(data));

            int outlen = 0;
            ToStr(senddata, ref buf, ref outlen);
            buflen = outlen;
            return buf;

        }
        // computerkey="9QG2CNZV"
        public Byte[] InitLoginPack(string username, string password, string computerkey, ref int buflen)
        {
            Byte[] buf = new Byte[0x100];

            int srcoffset = 0;
         
            Encoding ascii = Encoding.ASCII;

            Byte[] appdata = {0x0E,0x6E,0x00,0x00,0x00,0x02,0x26,0x2B,0x11};

            Buffer.BlockCopy(appdata, 0, buf, srcoffset, appdata.Length);
            srcoffset += appdata.Length;

            // 拷贝用户名
            Byte[] userlen = {0,Convert.ToByte(username.Length) };
            Buffer.BlockCopy(userlen, 0, buf, srcoffset, userlen.Length);
            srcoffset += userlen.Length;

            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(username)), 0, buf, srcoffset, username.Length);
            srcoffset += username.Length;

            // 常量数据
            Byte[] r1 = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x2E,0x35,0x82,0x26,0x12};
            Buffer.BlockCopy(r1, 0, buf, srcoffset, r1.Length);
            srcoffset += r1.Length;

            //string computerkey = "9QG2CNZV";
            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(computerkey)), 0, buf, srcoffset, computerkey.Length);
            srcoffset += computerkey.Length;

            string computerkeyother = "-7606-0000";            
            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(computerkeyother)), 0, buf, srcoffset, computerkeyother.Length);
            srcoffset += computerkeyother.Length;

            Byte[] r2 = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
            Buffer.BlockCopy(r2, 0, buf, srcoffset, r2.Length);
            srcoffset += r2.Length;

            Byte[] passwordlendata = { 0, Convert.ToByte(password.Length) };
            Buffer.BlockCopy(passwordlendata, 0, buf, srcoffset, passwordlendata.Length);
            srcoffset += passwordlendata.Length;


            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(password)), 0, buf, srcoffset, password.Length);
            srcoffset += password.Length;

            Byte[] passwordotherdata = { 0,0,0,0};
            Buffer.BlockCopy(passwordotherdata, 0, buf, srcoffset, passwordotherdata.Length);
            srcoffset += passwordotherdata.Length;

            Byte[] ipdatalen = { 0xD };
            Buffer.BlockCopy(ipdatalen, 0, buf, srcoffset, ipdatalen.Length);
            srcoffset += ipdatalen.Length;

            string localip = "192.168.1.2";
            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(localip)), 0, buf, srcoffset, localip.Length);
            srcoffset += localip.Length;

            Byte[] ipdataotherdata = { 0,0,0,0};
            Buffer.BlockCopy(ipdataotherdata, 0, buf, srcoffset, ipdataotherdata.Length);
            srcoffset += ipdataotherdata.Length;

            Byte[] r3 = { 0x4, 0xBD, 0xF0, 0xBB, 0xAA ,0,0,0,0,0,0};
            //Byte[] r3 = { 0x4, 0xB9, 0xE3, 0xD6, 0xDD, 0, 0, 0, 0, 0, 0 };
            Buffer.BlockCopy(r3, 0, buf, srcoffset, r3.Length);
            srcoffset += r3.Length;

            Byte[] citydata = { 0x06, 0x6A, 0x69, 0x6E, 0x68, 0x75, 0x61, 0, 0, 0, 0 };
            Buffer.BlockCopy(citydata, 0, buf, srcoffset, citydata.Length);
            srcoffset += citydata.Length;

            buflen = srcoffset;

            return buf;
        }
        public Byte[] InitLoginPack2(string username, string password, string computerkey, ref int buflen)
        {
            Byte[] buf = new Byte[0x100];

            int srcoffset = 0;

            Encoding ascii = Encoding.ASCII;

            Byte[] appdata = { 0x00,0xD1,0x00,0x00};
            Buffer.BlockCopy(appdata, 0, buf, srcoffset, appdata.Length);
            srcoffset += appdata.Length;

            Byte[] usernamelen = { 0, Convert.ToByte(username.Length) };
            Buffer.BlockCopy(usernamelen, 0, buf, srcoffset, usernamelen.Length);
            srcoffset += usernamelen.Length;

            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(username)), 0, buf, srcoffset, username.Length);
            srcoffset += username.Length;

            Byte[] r1 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            Buffer.BlockCopy(r1, 0, buf, srcoffset, r1.Length);
            srcoffset += r1.Length;

            Byte[] passwordlen = { 0, Convert.ToByte(password.Length) };
            Buffer.BlockCopy(passwordlen, 0, buf, srcoffset, passwordlen.Length);
            srcoffset += passwordlen.Length;

            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(password)), 0, buf, srcoffset, password.Length);
            srcoffset += password.Length;

            Byte[] passwordotherdata = { 0,0,0,0,0x12};
            Buffer.BlockCopy(passwordotherdata, 0, buf, srcoffset, passwordotherdata.Length);
            srcoffset += passwordotherdata.Length;

            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(computerkey)), 0, buf, srcoffset, computerkey.Length);
            srcoffset += computerkey.Length;

            string computerkeyother = "-7606-0000";
            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(computerkeyother)), 0, buf, srcoffset, computerkeyother.Length);
            srcoffset += computerkeyother.Length;
            
            Byte[] r2 = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x26,0x2B,0x00,0x00};
            Buffer.BlockCopy(r2, 0, buf, srcoffset, r2.Length);
            srcoffset += r2.Length;

            string localip = "192.168.1.2";
            Byte[] ipdatelen = { Convert.ToByte(localip.Length) };
            Buffer.BlockCopy(ipdatelen, 0, buf, srcoffset, ipdatelen.Length);
            srcoffset += ipdatelen.Length;

            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(localip)), 0, buf, srcoffset, localip.Length);
            srcoffset += localip.Length;

            Byte[] ipdataotherdata = { 0, 0, 0, 0 };
            Buffer.BlockCopy(ipdataotherdata, 0, buf, srcoffset, ipdataotherdata.Length);
            srcoffset += ipdataotherdata.Length;

            string szdate = "20020110-0309";

            Byte[] datelen = {Convert.ToByte(szdate.Length) };
            Buffer.BlockCopy(datelen, 0, buf, srcoffset, datelen.Length);
            srcoffset += datelen.Length;

            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(szdate)), 0, buf, srcoffset, szdate.Length);
            srcoffset += szdate.Length;

            Byte[] r3 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            Buffer.BlockCopy(r3, 0, buf, srcoffset, r3.Length);
            srcoffset += r3.Length;

            buflen = srcoffset;

            return buf;



        }
        public Byte[] InitTimeDate(String account)
        {
            Byte[] appdata = { 0x04, 0x24, 0x00, 0x03, 0x00, 0x04, 0x31, 0x36, 0x31, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            appdata[5] = Convert.ToByte(account.Length);
            Buffer.BlockCopy(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(account)), 0, appdata, 6, account.Length);

            return appdata;

        }
        public Byte[] InitTimeDate2()
        {
            Byte[] appdata = { 0x08,0x03,0x00,0x03,0x00,0x01};
            return appdata;

        }
        public void test()
        {
            string datastr = "33104915531437054E113C14360752273F164512511E2820551E34184E0C3B1A4925331E5103551A420B3C103A0C";

            Byte[] data = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(datastr));

            int outlen = 0;
            Byte[] outdata = new Byte[0x400];

            ToStr(data, ref outdata, ref outlen);


            Byte[] copydata = new Byte[outlen];

            Buffer.BlockCopy(outdata,0,copydata,0,outlen);

            UncrypteData(copydata);

        }

        public Byte[] GetWuliuByCity(String city)
        {

            Byte[] citydata = System.Text.Encoding.GetEncoding("gb2312").GetBytes(city);




            string senddata = "0B1301010004C4CFBEA9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003004C8ABB2BF00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000075EC4CFBEA92729";

            Byte[] data = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(senddata));
            

             int outlen = 0;
            Byte[] outdata = new Byte[0x400];

            ToStr(data, ref outdata, ref outlen);

            
            Byte[] copydata = new Byte[outlen];

            Buffer.BlockCopy(outdata,0,copydata,0,outlen);

            Buffer.BlockCopy(citydata, 0, copydata, 6, citydata.Length);
            Buffer.BlockCopy(citydata, 0, copydata, 0x110, citydata.Length);

            copydata[3] = this.Sendflag;
            return copydata;

            
        }
        public Byte[] getOldMsgPack(int startindex)
        {
            Byte[] pack = {6,7,0,this.Sendflag,0,0,0,0,0,1};

            int[] data = { startindex };
            Buffer.BlockCopy(data, 0, pack, 5, 4);

            return pack;


        }
        public String UncrypteData(Byte[] data)
        {
            bool bUncypt = false;

            if (data[0] == 0x20)
                return Encoding.Default.GetString(data);
            for (int e = 0; e < data.Length; e++)
            {
                if (data[e] > 0x40)
                    bUncypt = true;
            }

            if (!bUncypt)
                return Encoding.Default.GetString(data);
            string keytablestr = "18110216252707121E15052409100C041114160102177E0401151D10183E0B101E163C1C1A03251B1716071B2713100C15113C051A151C267E133E18101E1704011D031A1215171A031807241E0B04091A05272412102504111D09071A023E171B3C051E04171305161D11180312151D121009072706111C11103C121E171303150603110406170C1825130506141D1A1327101B26247E0C10150601261409160109151C04020B111A071D2611021304161C1E0B13160C7E151012172402250509127E181501032626041C130B25127E02170B061B7E1510051C18021B1E151D1013030B2518130412070209181B1C0C121006020C091603012617022715250111121003042409100C141711160211037E1B10151C05183E020B1E151D130318251B150718121302091B0C1C2616091A7E180401152506261C1314250B05021716101B1E153C1A121C251713071B27161015110C241A051C267E18133E1B1C1D120903070627021516103C1E05041315031C2706141A1324101B261D107E0C0601152614091625091C070415020B111A0B1E1B153C1A121C25108BC000000000";

            Byte[] keytabledata = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, Encoding.Unicode.GetBytes(keytablestr));

            int outlen = 0;
            Byte[] keytable = new Byte[0x400];

            ToStr(keytabledata, ref keytable, ref outlen);
            
            StringBuilder str = new StringBuilder();

            int index = 0;
            while(index < data.Length)
            {
                if (data[index] < 0x80)
                {
                    if (data[index] > 0x28)
                    {
                        Byte t1 = Convert.ToByte(DateTime.Now.Day);
                        Byte t2 = 0x29;
                        Byte t3 = data[index];
                        Byte d1 = Convert.ToByte(t3 - t2 - t1);
                        Byte t4 = Convert.ToByte(d1 + d1 * 4);
                        Byte t5 = 0;
                        while (t5 < 0xA)
                        {
                            Byte t6 = data[index + 1];
                            Byte t7 = keytable[t4 * 2 + t5];
                            if (t6 == t7)
                            {
                                t6 = Convert.ToByte(t5 + 0x30);

                                Char app = Convert.ToChar(t6);

                                str.Append(app);
                                break;

                            }
                            t5++;
                        }


                    }
                    else
                    {
                        str.Append(" ");
                    }

                    index = index + 2;
                }
                else
                    index++;

            }

            String call = str.ToString();

            return call;

        }
        public int  getMsgCount(int data)
        {
	        // 74,78,7c,80,84,88,8c,90,94,98
	        // 0  1  2  3  4  5  6, 7, 8,  9,
	        int[] basedata = {960,1200,2100,2800,4200,6800,8200,11900,17500,25500};
        										
	        bool bCrypteData = true;
	        if ( bCrypteData )
	        {
		        if ( data >= 2001 )
		        {
			        if ( data >= 6001 )
			        {
				        if ( (data - 6001) < 0x7D0 )
					        return data + basedata[6];
				        if ( (data - 8001) < 0x7D0 )
					        return data + basedata[7];
				        if ( (data - 10001) < 0x1388 )
					        return data +basedata[8];
			        }
			        else
			        {
				        if ( (data - 2001) < 0x7D0 )
					        return data + basedata[4];
				        if ( (data - 4001) < 0x7D0 )
					        return data + basedata[5];
			        }
		        }
		        else
		        {
			        if ( data >= 401 )
			        {
				        if ( (data - 401) < 0x258 )
					        return data + basedata[1];
				        if ( (data - 1001) < 0x1F4 )
					        return data + basedata[2];
				        if ( (data - 1501) < 0x1F4 )
					        return data + basedata[3];
			        }
			        else
			        {
				        if ( (data + 999999) < 0xF42A4 )
					        return data;
				        if ( (data - 101) < 0x12C )
					        return data + basedata[0];
			        }
		        }
		        return data + basedata[9];
	        }
	        return data;
        }

        void ToStr(Byte[] pDataStr,ref Byte[] pRetData,ref int outlen)
        {

	        Byte[] HexTable = {0,1,2,3,4,5,6,7,8,9,0xA,0xB,0xC,0xD,0xE,0xF,0xA,0xB,0xC,0xD,0xE,0xF};

	        //static TCHAR *HexTable = _T("0123456789ABCDEF");

            int len = pDataStr.Length;
	        int RetIndex = 0;
	        int i = 0;
            Byte dd1 = pDataStr[i];
	        while(i < pDataStr.Length && pDataStr[i] != 0)
	        {
                Byte h = 0, w = 0;
                Byte dd = HexTable[1];
               
		        if(pDataStr[i] > 0x40 && pDataStr[i] <= 0x46)
                    h = Convert.ToByte(HexTable[pDataStr[i] - 0x31] << 4);
		        else if(pDataStr[i] >= 0x30 && pDataStr[i] < 0x40)
                    h = Convert.ToByte(HexTable[pDataStr[i] - 0x30] << 4);
		        else 
		        {
			        i++;
			        continue;
		        }
		        i++;

		        if(pDataStr[i] >= 0x30 && pDataStr[i] < 0x40)
			        w = (HexTable[pDataStr[i] - 0x30]);
		        else if(pDataStr[i] > 0x40 && pDataStr[i] <= 0x46)
			        w = (HexTable[pDataStr[i] - 0x31]);
		        else
		        {
			        i++;
			        continue;
		        }
		        i++;


                pRetData[RetIndex] = Convert.ToByte(w + h);
		        RetIndex++;

	        }
	        outlen = RetIndex;
	        i++;
	        pRetData[i] = 0;
        }
    }
}
